"""
知识库管理服务使用示例
演示如何使用知识库管理服务进行知识的增删改查
"""

import asyncio
import sys
import os

# 添加项目根目录到Python路径
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from src.containers import get_knowledge_base_service
from src.services.knowledge_base_service import KnowledgeBaseItem, KnowledgeBaseQuery


async def main():
    """主函数，演示知识库管理服务的使用"""
    print("=== 知识库管理服务演示 ===")
    
    # 获取知识库服务实例
    kb_service = get_knowledge_base_service()
    
    # 1. 创建知识库条目
    print("\n1. 创建知识库条目")
    
    item1 = KnowledgeBaseItem(
        title="Python基础语法",
        content="Python是一种解释型、面向对象、动态数据类型的高级程序设计语言。",
        category="技术文档",
        tags=["Python", "编程语言", "基础"],
        metadata={"author": "张三", "difficulty": "初级"}
    )
    
    created_item1 = kb_service.create_knowledge_item(item1)
    print(f"创建条目: {created_item1.title}, ID: {created_item1.id}")
    
    item2 = KnowledgeBaseItem(
        title="FastAPI框架介绍",
        content="FastAPI是一个现代、快速（高性能）的Web框架，用于构建API，基于Python 3.7+的类型提示。",
        category="技术文档",
        tags=["FastAPI", "Web框架", "Python"],
        metadata={"author": "李四", "difficulty": "中级"}
    )
    
    created_item2 = kb_service.create_knowledge_item(item2)
    print(f"创建条目: {created_item2.title}, ID: {created_item2.id}")
    
    # 2. 查询知识库条目
    print("\n2. 查询知识库条目")
    
    if created_item1.id is not None:
        retrieved_item = kb_service.get_knowledge_item(created_item1.id)
        if retrieved_item:
            print(f"查询到条目: {retrieved_item.title}")
            print(f"内容: {retrieved_item.content}")
            print(f"分类: {retrieved_item.category}")
            print(f"标签: {retrieved_item.tags}")
            print(f"元数据: {retrieved_item.metadata}")
    else:
        print("创建的条目没有有效的ID")
    
    # 3. 搜索知识库条目
    print("\n3. 搜索知识库条目")
    
    # 按关键词搜索
    query1 = KnowledgeBaseQuery(query_text="Python", limit=5)
    results1 = kb_service.search_knowledge_items(query1)
    print(f"关键词 'Python' 搜索结果: {len(results1)} 条")
    for result in results1:
        print(f"  - {result.title}")
    
    # 按分类搜索
    query2 = KnowledgeBaseQuery(query_text="", category="技术文档", limit=5)
    results2 = kb_service.search_knowledge_items(query2)
    print(f"分类 '技术文档' 搜索结果: {len(results2)} 条")
    for result in results2:
        print(f"  - {result.title}")
    
    # 按标签搜索
    query3 = KnowledgeBaseQuery(query_text="", tags=["Python"], limit=5)
    results3 = kb_service.search_knowledge_items(query3)
    print(f"标签 'Python' 搜索结果: {len(results3)} 条")
    for result in results3:
        print(f"  - {result.title}")
    
    # 4. 更新知识库条目
    print("\n4. 更新知识库条目")
    
    if created_item1.id is not None:
        updated_item = created_item1
        updated_item.content = "Python是一种解释型、面向对象、函数式编程、动态数据类型的高级程序设计语言。"
        updated_item.tags.append("函数式编程")
        updated_item.version += 1
        
        result = kb_service.update_knowledge_item(created_item1.id, updated_item)
        print(f"更新条目: {result.title}")
        print(f"新内容: {result.content}")
        print(f"新标签: {result.tags}")
        print(f"新版本: {result.version}")
    
    # 5. 再次查询验证更新
    print("\n5. 验证更新结果")
    
    if created_item1.id is not None:
        verified_item = kb_service.get_knowledge_item(created_item1.id)
        if verified_item:
            print(f"验证条目: {verified_item.title}")
            print(f"内容: {verified_item.content}")
            print(f"标签: {verified_item.tags}")
            print(f"版本: {verified_item.version}")
    
    # 6. 删除知识库条目
    print("\n6. 删除知识库条目")
    
    delete_result1 = False
    delete_result2 = False
    
    if created_item1.id is not None:
        delete_result1 = kb_service.delete_knowledge_item(created_item1.id)
    print(f"删除条目1结果: {delete_result1}")
    
    if created_item2.id is not None:
        delete_result2 = kb_service.delete_knowledge_item(created_item2.id)
    print(f"删除条目2结果: {delete_result2}")
    
    # 7. 验证删除
    print("\n7. 验证删除结果")
    
    if created_item1.id is not None:
        deleted_item1 = kb_service.get_knowledge_item(created_item1.id)
        print(f"删除后查询条目1: {'存在' if deleted_item1 else '不存在'}")
    
    if created_item2.id is not None:
        deleted_item2 = kb_service.get_knowledge_item(created_item2.id)
        print(f"删除后查询条目2: {'存在' if deleted_item2 else '不存在'}")
    
    print("\n=== 演示完成 ===")


if __name__ == "__main__":
    asyncio.run(main())